#! /bin/bash

#Zinud-skel, configure and initialize ZinuD user account
#Version 0.2.2

# +------------------------------------------------------------+
# | mermouy@gmail.com
# |
# | This program is free software; you can redistribute it and/or
# | modify it under the terms of the GNU General Public License
# | as published by the Free Software Foundation; either version
# | 3 of the License, or (at your option) any later version.
# | 
# | This program is distributed in the hope that it will be useful,
# | but WITHOUT ANY WARRANTY; without even the implied warranty
# | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# | See the GNU General Public License for more details.
# |
# | You should have received a copy of the GNU General Public
# | License along with this program; if not, write to the
# | Free Software Foundation, Inc., 51 Franklin St,
# | Fifth Floor, Boston, MA  02110-1301  USA
# +------------------------------------------------------------+

# VARIABLES
dud="/home/$1"
dub="$dud/.config"
dua="$dub/zinud"
duc="$dub/compiz"
Autodir="$dub/autostart"
zinrep="/usr/share/zinud"
tempdiag="/tmp/tmpdiag"

		########              Fonctions       ########

### FONCTION CONFIGURATION UTILISATEUR ###

function Zinudskel (){
#Création des dossiers de config
	mkdir -vp $dua
	mkdir -vp $dub/Thunar
	mkdir -vp $dub/cairo-dock
	mkdir -vp $duc
	mkdir -vp $Autodir
#Copie des fichier de config zinud de l'utilisateur
	cd $zinrep/zinud/
	cp -vf *.sh $dua/
	chmod +x $dua/*.sh
	cp -vf zinudfirstboot $dua/.zinudfirstboot.sh
# Thunar
	if [ -f $dub/Thunar/uca.xml ]
		then mv $dub/Thunar/uca.xml $dub/Thunar/uca.xml~
	fi
	cp -vf uca.xml $dub/Thunar/	
	cp -vf thunarrc $dub/Thunar/
# Bashrc
	if [ -f $dud/.bashrc ]
		then mv $dud/.bashrc $dud/.bashrc~
		cp -vf bashrc $dud/.bashrc
	fi
# Bash aliases
	if [ -f $dud/.bash_aliases ]
		then mv -v $dud/.bash_aliases $dud/.bash_aliases~
	fi
	cp -vf bash_aliases $dud/.bash_aliases
# Gtkrc
	if [ -f $dud/.gtkrc-2.0 ]
		then mv -v $dud/.gtkrc-2.0 $dud/.gtkrc-2.0~
	fi
	cp -vf gtkrc-2.0 $dud/.gtkrc-2.0
# Roxterm
	if [ -d $dub/roxterm.sourceforge.net ]
		then mv -v $dub/roxterm.sourceforge.net $dub/roxterm.sourceforge.net.old
	fi
	cd ..
	cp -rvf roxterm.sourceforge.net $dub/
# Nitrogen
	if [ -e /usr/bin/nitrogen ]
		then cp -rvf nitrogen $dub/
	fi
# Cairo-dock
	cd $dub/cairo-dock/
	mv -vf current_theme current_theme.old
	wget zinud.youm.org/zinud.theme.tar.bz2
	tar -xjf zinud.theme.tar.bz2
	mv -v zinud.theme current_theme
	chmod -R 755 $dub/
	cd $duc/
# Compiz config
	cp -rvf $zinrep/compizconfig/ .
	chown -R $1:$1 $dud

#Ajout de l'utilisateur aux groupes système

function Addgroups () {
gruplist="plugdev haldaemon users powerdev disk cdrom audio dialout dip fuse games lp lpadmin video disk sudo"
for i in $gruplist
do
	adduser $1 $i
done
}

		#######   Fin des déclaration des fonctions   #######
  

# Vérification de l'entrée utilisateur

if [ -z $1 ]
then
dialog --yesno "$Userinitquest" 12 50
	if [ $? = "0" ]
	then
		dialog --title "$Confinit" --inputbox "$Enteruser"  10 45 2>$tempdiag
		utilisateur=$(cat $tempdiag)
# Relance du script avec un utilisateur en argument
		exec $0 $utilisateur
	else
		exit 0
	fi
fi
Userlistexist=$(cat /etc/passwd | cut -d: -f1 | grep $1)
if [ $1 = "root" ] 
then
dialog  --title "$Confinit" --inputbox "$Enteruserroot"  10 45 2>$tempdiag
	utilisateur=$(cat $tempdiag)
	exec $0 $utilisateur
fi
if [ -d /home/$1/ ] && [ ! -z $Userlistexist ]
then

# Configuration Squelette Utilisateur

Zinudskel $1

#Ajout de l'utilisateur au groupes systemes

Addgroups $1

# Choix du mode de connexion

exec /usr/local/sbin/login-configurator $1

#Si utilisateur non-existant
else
	dialog --title "Configuration initiale" --inputbox "L'utilisateur sélectionné n'existe pas! Entrez un utilisateur existant:	" 10 45 2>$tempdiag
	utilisateur=$(cat $tempdiag)
	exec $0 $utilisateur
fi
